package com.zhuoyue.biz.core.api.domain;

import com.baomidou.mybatisplus.annotation.*;
import com.zhuoyue.biz.core.api.domain.base.BaseDoClass;
import com.zhuoyue.biz.core.api.dto.req.QuickLoginReqDTO;
import com.zhuoyue.biz.core.api.dto.req.UserReqDTO;
import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;
import com.zhuoyue.common.utils.Md5;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;


/**
 * APP用户实体类
 *
 * @author Administrator
 */
@Data
@TableName(value = "`tbl_app_user`")
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class AppUserDO extends BaseDoClass {

    @TableField(value = "`account`")
    private String account;

    @TableField(value = "`password`")
    private String password;

    @TableField(value = "`nick_name`")
    private String nickName;

    @TableField(value = "`img_url`")
    private String imgUrl;

    @TableField(value = "`token`")
    private String token;

    @TableField(value = "`source_type`")
    private Integer sourceType;

    @TableField(value = "`source_id`")
    private String sourceId;

    @TableField(value = "`tsbz`")
    private String tsbz;
    /**
     * 微信快速登录
     */
    @TableField(value = "`openid_weixin`")
    private String openidWeiXin;
    /**
     * qq快速登录
     */
    @TableField(value = "`openid_qq`")
    private String openidQQ;

    @TableField(value = "`openid_apple`")
    private String openidApple;

    /**
     * 性别
     */
    @TableField(value = "`gender`")
    private String gender;

    /**
     * 快速登录类型0，微信 1，QQ
     */
    @TableField(exist = false)
    private String type;

    @TableField(value = "`organization_code`")
    private String organizationCode;

    /**
     * 用户DTO信息转化为DO信息
     *
     * @param userReqDTO
     * @param orgCode
     * @return
     */
    public AppUserDO loginDtoToAppUserDo(UserReqDTO userReqDTO, String orgCode) {
        try {
            this.password = Md5.getMd5(userReqDTO.getPassword());
        } catch (Exception e) {
            throw new ZyServerException(ErrorType.Service_Fail.getCode(), "加密异常");
        }
        this.account = userReqDTO.getAccount();
        this.organizationCode = orgCode;
        this.sourceId = userReqDTO.getSourceId();
        this.type = userReqDTO.getType();
        this.sourceType = Integer.parseInt(userReqDTO.getSourceType());
        return this;
    }

    /**
     * 用户DTO信息转化为DO信息
     *
     * @param userReqDTO
     * @param orgCode
     * @return
     */
    public AppUserDO registerDtoToAppUserDo(UserReqDTO userReqDTO, String orgCode) {
        try {
            this.password = Md5.getMd5(userReqDTO.getPassword());
        } catch (Exception e) {
            throw new ZyServerException(ErrorType.Service_Fail.getCode(), "加密异常");
        }
        this.account = userReqDTO.getAccount();
        this.nickName = "小e";
        this.imgUrl = "http://www.img.zyzncloud.com/UserHeadImg/default.jpg";
        this.organizationCode = orgCode;
        this.sourceId = userReqDTO.getSourceId();
        this.type = userReqDTO.getType();
        this.sourceType = Integer.parseInt(userReqDTO.getSourceType());
        return this;
    }

    /**
     * @Description: 快速登陆对象信息
     * @param: quickLoginReqDTO
     * @param: orgCode
     * @return: com.zhuoyue.biz.core.api.domain.AppUserDO
     * @author:wjming
     * @Date:2021-06-29 19:23
     */
    public AppUserDO quickLoginDtoToAppUserDo(QuickLoginReqDTO quickLoginReqDTO, String orgCode) {
        this.organizationCode = orgCode;
        this.sourceId = quickLoginReqDTO.getSourceId();
        this.type = quickLoginReqDTO.getType();
        this.sourceType = Integer.parseInt(quickLoginReqDTO.getSourceType());
        return this;
    }

    public AppUserDO modifyUserBaseMessage(UserReqDTO userReqDTO, String id) {
        if (userReqDTO == null) {

        }
        setId(id);
        this.imgUrl = userReqDTO.getImgUrl();
        this.nickName = userReqDTO.getNickName();
        return this;
    }
}
